package com.chao.base.string;

import java.util.HashMap;

/**
 * @author : Rookie
 * @description :
 * @date : 2022/10/29
 */
public class StringOperateTest {

    public static void main(String[] args) {
        StringTest(10000);
        StringBufferTest(10000);
        StringBuilderTest(10000);
    }
    public static void StringTest(int n){
        String str = "";
        Long startTime = System.currentTimeMillis();
        for(int i=0;i<n;i++){
            str += i;
        }
        Long endTime = System.currentTimeMillis();
        System.out.println("String 连接"+ n +"次消耗"+(endTime-startTime)+"ms");
    }
    public static void StringBufferTest(int n){
        StringBuffer str = new StringBuffer();
        Long startTime = System.currentTimeMillis();
        for(int i=0;i<n;i++){
            str.append(i);
        }
        Long endTime = System.currentTimeMillis();
        System.out.println("StringBuffer 连接"+ n +"次消耗"+(endTime-startTime)+"ms");
    }
    public static void StringBuilderTest(int n){
        StringBuilder str = new StringBuilder();
        Long startTime = System.currentTimeMillis();
        for(int i=0;i<n;i++){
            str.append(i);
        }
        Long endTime = System.currentTimeMillis();
        System.out.println("StringBuilder 连接"+ n +"次消耗"+(endTime-startTime)+"ms");
    }
}
